import { Injectable } from '@nestjs/common';
import { ConfigService as NestConfigService } from '@nestjs/config';

@Injectable()
export class ConfigService {
  constructor(private configService: NestConfigService) {}

  /**
   * Get a configuration value by key
   * @param key The configuration key
   * @param defaultValue Optional default value if key doesn't exist
   */
  get<T = any>(key: string, defaultValue?: T): T {
    const value = this.configService.get<T>(key);
    return value !== undefined ? value : (defaultValue as T);
  }

  /**
   * Get a nested configuration value
   * @param path The path to the configuration value (e.g., 'database.host')
   * @param defaultValue Optional default value if path doesn't exist
   */
  getOrDefault<T = any>(path: string, defaultValue: T): T {
    const value = this.configService.get<T>(path);
    return value !== undefined ? value : defaultValue;
  }

  /**
   * Check if a configuration key exists
   * @param key The configuration key
   */
  has(key: string): boolean {
    return this.configService.get(key) !== undefined;
  }

  /**
   * Get all configuration values
   */
  getAll(): Record<string, any> {
    const config = this.configService.get<Record<string, any>>('');
    return config || {};
  }

  /**
   * Get the current environment
   */
  getNodeEnv(): string {
    return this.get<string>('NODE_ENV', 'development');
  }

  /**
   * Check if the current environment is production
   */
  isProduction(): boolean {
    return this.getNodeEnv() === 'production';
  }

  /**
   * Check if the current environment is development
   */
  isDevelopment(): boolean {
    return this.getNodeEnv() === 'development';
  }

  /**
   * Check if the current environment is test
   */
  isTest(): boolean {
    return this.getNodeEnv() === 'test';
  }
}
